package com.yx.service.init;

import com.yx.annotation.ShiroDesc;
import com.yx.entity.SysPermission;
import com.yx.mapper.SysPermissionMapper;
import com.yx.mapper.SysRoleMapper;
import com.yx.mapper.SysRolePermissionMapper;
import com.yx.sys.SysConst;
import com.yx.util.ScanPackeageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.*;

/**
 * @Description: <br/>
 * 系统初始化
 * <p>
 * <br/>
 * @Auther: zhangqi
 * @create 2019/11/7 10:57
 */
@Component
@Slf4j
public class InitSysPermissionAndRole {

    /**
     * 系统角色权限
     */
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    /**
     * 系统权限
     */
    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    /**
     * 系统角色
     */
    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 所有方法上的权限
     */
    private static Map<String, String> ALL_PERMISSIONS = new HashMap<>();

    /**
     * 所有方法上的角色
     */
    private static final Map<String, String> ALL_ROLES = new HashMap<>();

    /**
     * 没有权限描述的默认描述
     */
    private static final String STATIC_PERMISSION_DESC = "系统添加";

    /**
     * 系统初始化方法
     */
    public void init() throws Exception {
        // 扫描系统根包
        Set<Class<?>> classSet = ScanPackeageUtils.getClasses(SysConst.SYS_BASE_PKG);

        for (Class<?> class1 : classSet) {
            Method[] methods = class1.getMethods();
            for (Method method : methods) {
                RequiresPermissions rp = method.getAnnotation(RequiresPermissions.class);
                if (null != rp && null != rp.value()) {
                    for (String permission : rp.value()) {
                        if (null != permission && !permission.endsWith("*")) {
                            ShiroDesc desc = method.getAnnotation(ShiroDesc.class);
                            String val = ALL_PERMISSIONS.get(permission);
                            if (null != desc) {
                                val = desc.value();
                            }
                            if (null == val) {
                                val = STATIC_PERMISSION_DESC;
                            }
                            ALL_PERMISSIONS.put(permission, val);
                        }
                    }
                }
                RequiresRoles rr = method.getAnnotation(RequiresRoles.class);
                if (null != rr && null != rr.value()) {
                    for (String role : rr.value()) {
                        ALL_ROLES.put(role, STATIC_PERMISSION_DESC + " : " + role);
                    }
                }
            }
        }
        // 添加删除权限
        initPermission();
    }


    /**
     * 添加删除权限
     */
    private void initPermission(){
        List<SysPermission> all = sysPermissionMapper.getAllPermissions();
        for (SysPermission permission : all) {
            if (!ALL_PERMISSIONS.containsKey(permission.getPermId()) && !permission.getPermId().startsWith("menu:")) {
                sysPermissionMapper.delete(permission.getPermId());
            }
        }
        // 添加或跟更新权限
        for (String permId : ALL_PERMISSIONS.keySet()) {
            SysPermission permission = sysPermissionMapper.getInfo(permId);
            if (Objects.isNull(permission)) {
                permission = new SysPermission();
                permission.setPermId(permId);
                permission.setPermName(ALL_PERMISSIONS.get(permId));
                sysPermissionMapper.insert(permission);
            } else {
                permission.setPermName(ALL_PERMISSIONS.get(permId));
                sysPermissionMapper.update(permission);
            }
        }
    }

}
